package org.jeecg.modules.door.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.mapper.LwgyBaseApartmentMapper;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.door.entity.LwgyEglock;
import org.jeecg.modules.door.entity.LwgyEglockRoomUser;
import org.jeecg.modules.door.service.ILwgyEglockService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
* @Description: 门锁分组管理
* @Author: jeecg-boot
* @Date:   2022-06-15
* @Version: V1.0
*/
@Api(tags="门锁管理")
@RestController
@RequestMapping("/door/lwgyEglock")
@Slf4j
public class LwgyEglockController extends JeecgController<LwgyEglock, ILwgyEglockService> {

   @Autowired
   private ILwgyEglockService lwgyEglockService;
   @Autowired
   private ILwgyBaseApartmentService lwgyBaseApartmentService;
   @Autowired
   private ILwgyBaseBedService lwgyBaseBedService;

   /**
    * 自定义分页列表查询
    *
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "门锁管理-分页列表查询")
   @ApiOperation(value = "门锁管理-分页列表查询", notes = "门锁管理-分页列表查询")
   @GetMapping(value = "/listCustom")
   public Result<?> queryPageListCustom(
                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(name = "type",required = false) String type,
                                        @RequestParam(name = "id",required = false) String id,
                                        HttpServletRequest req) {
      LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentService.getById(id);
      type = lwgyBaseApartment.getType();
      LwgyEglock lwgyEglock = new LwgyEglock();
      QueryWrapper<LwgyEglock> queryWrapper = QueryGenerator.initQueryWrapper(lwgyEglock, req.getParameterMap());
      Page<LwgyEglock> page = new Page<>(pageNo, pageSize);

      if (type != null) {
         if ("school".equals(type)) {
            queryWrapper.eq("apartment4.id",id);
         } else if ("campus".equals(type)) {
            queryWrapper.eq("apartment4.id",id);
         } else if ("building".equals(type)) {
            queryWrapper.eq("apartment3.id",id);
         } else if ("unit".equals(type)) {
            queryWrapper.eq("apartment2.id",id);
         } else if ("layer".equals(type)) {
            queryWrapper.eq("apartment1.id",id);
         } else if ("room".equals(type)) {
            queryWrapper.eq("apartment.id",id);
         }
      }

      queryWrapper.eq("apartment.type","room");

      IPage<LwgyEglock> pageList2 = lwgyEglockService.queryPageListCustom(page, queryWrapper);
      return Result.OK(pageList2);
   }
   /**
    * 修改门锁标识
    *
    * @return
    */
   @AutoLog(value = "门锁管理-修改门锁标识")
   @ApiOperation(value = "门锁管理-修改门锁标识", notes = "门锁管理-修改门锁标识")
   @RequestMapping(value = "/editDoorCode")
   public Result<?>  editDoorCode(LwgyEglock lwgyEglock) {
      lwgyEglockService.updateDoorCodeByRoomId(lwgyEglock);
      return Result.OK("编辑成功!");
   }
   /**
    * 查询某个房间绑定的人员信息
    *
    * @return
    */
   @AutoLog(value = "门锁管理-查询某个房间绑定的人员信息")
   @ApiOperation(value = "门锁管理-查询某个房间绑定的人员信息", notes = "门锁管理-查询某个房间绑定的人员信息")
   @GetMapping(value = "/selectUserByRoom")
   public Result<?>  selectUserByRoom(String roomId) {
      List<LwgyEglockRoomUser> list = lwgyEglockService.selectUserByRoom(roomId);
      return Result.OK(list);
   }

   /**
    * 导出excel
    *
    * @param request
    * @param
    */
   @RequestMapping(value = "/exportXlsDoorInfo")
   public ModelAndView exportXlsDoorInfo(HttpServletRequest request, LwgyEglock lwgyEglock) {
/*      List<ImportStuHotel> lwgyBaseBed = lwgyHotelArrangementService.outStuHotel(lwgyBed);*/
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
    /*  ImportStuHotel importStuHotel = new ImportStuHotel();
  */
    //获取门锁数据
      List<LwgyEglock> list = lwgyEglockService.queryDoorList(lwgyEglock);
      mv.addObject(NormalExcelConstants.FILE_NAME, "门锁信息"); //此处设置的filename无效 ,前端会重更新设置一下
      mv.addObject(NormalExcelConstants.CLASS, LwgyEglock.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("门锁信息", ""));
      mv.addObject(NormalExcelConstants.DATA_LIST, list);
      return mv;
   }

}
